<template>
  <div class="myContainer">
    <!-- 头部标题 start -->  
    <el-row class="myHeadeTitle">
      <el-col :span="18" class="myLineHeight_35px">
        {{title}}
      </el-col>
      <el-col :span="6" class="myTextAlignRight">
        <el-button class="myBgGreen" @click="onAdd" v-if="isShow(239)">新增</el-button>
        <!-- <el-button class="myBgGreen">导出</el-button> -->
      </el-col>
    </el-row>
    <!-- 头部标题 end -->
    <!-- 搜索部分 start -->
    <el-row :gutter="10">
      <el-col :span="7">
        <template>
          <el-cascader
            placeholder="全部项目/小区"
            :options="regionOptions"
            v-model="regionOptSelected"
            ></el-cascader>
          </el-select>
        </template>
      </el-col>
      <el-col :span="6">
        <el-button class="myBgGreen" @click="onSearchKW">查询</el-button>
      </el-col>
    </el-row>
    <!-- 搜索部分 end -->
    <!-- 数据列表 start -->
    <div class="myPaddingTop_10px">
      <template>
        <el-table
          :data="tableDataList"
          border
          stripe
          v-loading="loading"
          @selection-change="handleSelectionChange"
          style="width: 100%">
          <!-- <el-table-column
            type="selection"
            width="100">
          </el-table-column> -->
          <el-table-column
            type="index"
            label="序号"
            width="100">
          </el-table-column>
          <el-table-column
            prop="id"
            label="任务编号">
          </el-table-column>
          <el-table-column
            prop="name"
            label="任务名称">
          </el-table-column>
          <el-table-column
            prop="count"
            width="100"
            label="巡检次数">
          </el-table-column>
          <el-table-column
            prop="status"
            width="100"
            label="状态">
            <template scope="scope">
              <span v-if="scope.row.status==0">禁用</span>
              <span v-else>启用</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="area_name"
            label="所属小区">
          </el-table-column>
          <el-table-column
            width="100"
            label="操作">
            <template scope="scope">
              <el-button class="look" type="text primary" size="small" @click="onDetail(scope.row.id)" v-if="isShow(240)">编辑</el-button> |
              <el-button class="look" type="text warning" size="small" @click="onDelete(scope.row.id)" v-if="isShow(241)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- 数据列表 end -->
    <!-- 分页控件 start -->
    <div class="block">                    
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page_num"
        :page-sizes="[10, 15]"
        :page-size="page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!-- 分页控件 end -->
  </div>
</template>  

<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myHeadeTitle{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    padding-bottom: 5px;
    border-bottom: 1px solid #dbdbdb;
    margin-bottom:10px; 
  }
  .myHeadeTitle:before,.myHeadeTitle:after{
    content: " ";
    display: block;
    clear: both;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top: 2px;
  }
  .myLineHeight_35px{
    line-height: 35px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }
</style>

<script>
  import { getInspectionList , deleteInspection } from '@/api/routinginspectionplan';
  import { getRegion } from '@/utils/localstorage';
  import { extractAreaIds } from '@/utils/misc';
  export default {
     data(){
      return{
        title:"日常巡检计划管理",
        tableDataList:[],//数据列表
        page_size:10,
        page_num:1,
        total:null,
        regionOptions: [],
        regionOptSelected: [],
        kw:"",
        loading:false
      }
    },
    components: {
       
    },
    mounted(){
      document.title = this.title;

      this.regionOptions = getRegion();
      this.regionOptSelected.push(this.regionOptions[0].value,this.regionOptions[0].children[0].value,this.regionOptions[0].children[0].children[0].value);
      
      this.onGetInspectionList();
    },
    computed: {
      areaIds() {
        return extractAreaIds(this.regionOptSelected, this.regionOptions);
      }
    },
    methods:{
      isArray(o){
        return Object.prototype.toString.call(o)=='[object Array]';
      },
      onGetInspectionList(){//获取巡检计划模板列表
        let vm=this;
        vm.loading=true;
        const params = {
          area_id:vm.areaIds,
          //keyword:vm.kw,
          page_num: vm.page_num,
          page_size: vm.page_size
        };
        getInspectionList(params)
        .then(response => {
          let res = response.data;
          vm.tableDataList = res.rows;
          vm.total = res.total;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;
          vm.loading=false;
          // console.log("拉取成功",vm.tableDataList);
        }).catch(error => {
          vm.loading=false;
          console.log("拉取失败",error);
        })
      },
      handleSelectionChange(val) {//添加表格复选框【全选等功能】
        
      },
      handleSizeChange(val) {//每页条数控制
        this.page_size = val;
        this.onGetInspectionList();
      },
      handleCurrentChange(val) {//每页
        this.page_num = val;
        this.onGetInspectionList();
      },
      onAdd:function(){
        this.$router.push({
          path:'/inspectionTaskManageComponents',
          query: {type: 'add'}
        })
      },
      onSearchKW(){//关键词搜索
        this.onGetInspectionList();
      },
      onDelete(plan_id){//删除
        let vm=this;
        vm.$confirm('您确定要删除这条数据吗？', '提示', {//弹出框
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteInspection(plan_id).then(response => {
            let res=response.data;
            vm.$message.success("执行成功！");
            this.onGetInspectionList();
            // console.log("ok")
          }).catch(error => {
            vm.$message.success("执行失败！");
          })
        }).catch(() => {
          vm.$message({
            type: 'info',
            message: '已取消执行'
          });          
        });
      },
      onDetail(plan_id){//编辑
        this.$router.push({
          path:'/inspectionTaskManageComponents',
          query: {type: 'update',id:plan_id}
        })
      }

    }
  }

</script>  



